﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infinity_World
{
    /// <summary>
    /// Prepare the map and generates natural Structures
    /// </summary>
    static class MapCreator
    {
        // Returns a prepared map
        public static Map GetWorld(UInt64 x, UInt64 y, UInt64 depht)
        {
            // Generate a plain map
            Map map = new Map(x, y, depht);

            // Generate each level
            for (UInt64 i = 0; i <= (depht - 1); i++)
            {

                // If lowest level will be generated
                if (i == 0)
                {
                    // Use special function to generate lowest level
                    map = GenerateLowestLevel(map, x, y);
                }

                // If an other level will be generated
                else
                {
                    // Use special function to generate a level
                    map = GenerateRandomLevel(map, x, y, i, 50);
                }
            }
            return map;
        }

        private static Map GenerateLowestLevel(Map map, UInt64 x, UInt64 y)
        {
            for (UInt64 i = 0; i <= (x - 1); i++)
            { 
                for (UInt64 j = 0; j <= (y - 1); j++)
                {
                    map.MapArray[i, j, 0] = new Block(MaterialType.stone);
                }
            }
            return map;
        }

        private static Map GenerateRandomLevel(Map map, UInt64 x, UInt64 y, UInt64 depht, int boundary)
        {
            Random rand = new Random();
            for (UInt64 i = 0; i <= (x - 1); i++)
            {
                for (UInt64 j = 0; j <= (y - 1); j++)
                {
                    int num = rand.Next(1, 100);
                    if (num <= boundary)
                    {
                        map.MapArray[i, j, depht] = new Block(MaterialType.stone);
                    }
                    else
                    {
                        map.MapArray[i, j, depht] = new Block(MaterialType.air);
                    }
                }
            }
            return map;
        }
    }
}
